/**
 * 
 */
package com.jushu.storbox.actions;

import java.io.UnsupportedEncodingException;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.constant.MessageCode;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.service.ICloudDriveService;
import com.jushu.storbox.util.LogUtil;
import com.jushu.storbox.util.PublicTools;
import com.huawei.sharedrive.sdk.android.exception.ClientException;
import com.huawei.sharedrive.sdk.android.modelV2.request.LinkCreateRequestV2;
import com.huawei.sharedrive.sdk.android.serviceV2.MailClientV2;

/**
 * Link module actions
 */
public class LinkAction extends Actions {
	
	private final static String TAG = "LinkAction";

	public LinkAction(Context context) {
		super(context);
	}

	/**
	 * Cancel link
	 * @param getContext()
	 * @param service
	 * @param exceptionHandler
	 * @param fileInformation
	 * @return
	 */
	public boolean removeLinkShare(ICloudDriveService service,Handler exceptionHandler,
			FileFolderInfo fileInformation) {
		LogUtil.i(TAG, "removeLinkShare[Actions]");
		if (!checkToken(service, exceptionHandler)) {
			return false;
		}
//		try {
//			service.deleteLink(fileInformation);
//		} catch (ClientException e) {
//			return false;
//		}
		return true;
	}
	
	/**
	 * async request, ignore result
	 * @param getContext()
	 * @param service
	 * @param fileInformation
	 * @param linkRequest
	 */
	public void updateLink(ICloudDriveService service,
			FileFolderInfo fileInformation, LinkCreateRequestV2 linkRequest) {
		LogUtil.i(TAG, "setLink[Actions]");
		try {
			if (!checkToken(service, null)) {
				return;
			}
			service.updateLink(fileInformation, linkRequest);
		} catch (Exception e) {
			LogUtil.e(TAG,  e.getMessage());
		}
	}
	
	/**
	 * send link notify mail
	 * @param getContext()
	 * @param service
	 * @param exceptionHandler
	 * @param emailList
	 * @param message
	 * @param nodeName
	 * @param linkUrl
	 * @param plainAccessCode
	 */
	public boolean sendMail(ICloudDriveService service,Handler exceptionHandler, 
			List<String> emailList, String message,String nodeName,String linkUrl,String plainAccessCode) {
		LogUtil.i(TAG, "setLink[Actions]");
		try {
			if (!checkToken(service, null)) {
				return false;
			}
			MailClientV2.getInstance().sendLinkMail(ShareDriveApplication.getInstance()
		            .getAuthorization(), emailList, message, nodeName, PublicTools
					.getClientUserName(getContext()), linkUrl, plainAccessCode);
			return true;
		} catch (ClientException e) {
			LogUtil.e(TAG, e.getCode() + "_" + e.getStatusCode());
			Message uiMsg = new Message();
			uiMsg.what = e.getStatusCode();
			uiMsg.getData().putString("code", e.getCode());
			exceptionHandler.sendMessage(uiMsg);
		} catch (UnsupportedEncodingException e) {
			LogUtil.e(TAG, e.getMessage());
			Message uiMsg = new Message();
			uiMsg.what = MessageCode.LINK_MAIL_FAIL;
			exceptionHandler.sendMessage(uiMsg);
		}
		return false;
	}
}
